package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Article;
import org.rcloud.io.model.Homestay;
import org.rcloud.io.service.ArticleService;
import org.rcloud.io.service.HomestayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/homestay")
public class HomestayController {

    @Autowired
    private HomestayService homestayService;

    @Autowired
    private ArticleService articleService;

    /*
    * 获取所有的精品民宿信息
    * */
    @ApiOperation(value = "获取所有的精品民宿信息", notes = "获取所有的精品民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findHomestay", produces = {"application/json;charset=UTF-8"})
    public PageVo<Homestay> findHomestay() {
        return homestayService.findHomestay();
    }

    /*
    * 根据eid获取精品民宿信息
    * */
    @ApiOperation(value = "根据eid获取精品民宿信息", notes = "根据eid获取精品民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        return homestayService.findByEid(object.getString("eid"));
    }

    /*
    * 保存精品民宿信息
    * */
    @ApiOperation(value = "保存精品民宿信息", notes = "保存精品民宿信息")
    @ResponseBody
    @RequestMapping(value = "/saveHomestay", produces = {"application/json;charset=UTF-8"})
    public boolean saveHomestay(@RequestBody String params) {

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        String nameTitle = name+System.currentTimeMillis();//生成时间戳,确保两张表能够关联

        Homestay homestay = new Homestay();
        homestay.setEid(eid);
        homestay.setName(name);
        homestay.setAddress(address);
        homestay.setLongitude(longitude);
        homestay.setLatitude(latitude);
        homestay.setEtype(etype);
        homestay.setSummary(summary);
        homestay.setReport(report);
        homestay.setUsername(username);
        homestay.setUserphone(userphone);
        homestay.setInf02(nameTitle);
        homestay.setFlag(1);

        Article article = new Article();
        article.setTitle(nameTitle);

        int end = Integer.valueOf(homestayService.saveHomestay(homestay).getInfo().toString());
        if(end!=1){
            return false;
        }

        //保存文章
        int art = Integer.valueOf(articleService.save(article).getInfo().toString());
        //        System.out.println("将文章的数据保存,状态为:"+art);
        if(art!=1){
            return false;
        }

        String INF01 = articleService.findTitle(nameTitle).getInfo().toString();
        //        System.out.println("查找文章的id为="+shouming);

        int upd = Integer.valueOf(homestayService.updateArtid(nameTitle,INF01).getInfo().toString());
//        System.out.println("修改刚刚保存的信息,就是将文章id放代言家庭到表中,返回的状态为="+upd);
        if (upd!=1){
            return false;
        }

        return true;
    }

    /*
    * 根据id获取精品民宿信息
    * */
    @ApiOperation(value = "根据id获取精品民宿信息", notes = "根据id获取精品民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(@RequestBody String params) {
        String[] ids = params.split("=");
        int id = Integer.parseInt(ids[1]);
        return homestayService.findById(id);
    }

    /*
     * 修改精品民宿信息
     * */
    @ApiOperation(value = "修改精品民宿信息", notes = "修改精品民宿信息")
    @ResponseBody
    @RequestMapping(value = "/updateHomestay", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateHomestay(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Homestay homestay = new Homestay();
        homestay.setId(id);
        homestay.setEid(eid);
        homestay.setName(name);
        homestay.setAddress(address);
        homestay.setLongitude(longitude);
        homestay.setLatitude(latitude);
        homestay.setEtype(etype);
        homestay.setSummary(summary);
        homestay.setReport(report);
        homestay.setUsername(username);
        homestay.setUserphone(userphone);

        return homestayService.updateHomestay(homestay);
    }
}